﻿using Beway.ValueAdded.Model;
using Beway.ValueAdded.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Beway.ValueAdded.RechargeGateway.Controllers
{
    public class BaseController:Controller
    {
        protected RechargeServ rs = SingletonProvider<RechargeServ>.Instance;
        AdminUserServ admSvc = SingletonProvider<AdminUserServ>.Instance;
        SmsLogSvc smsLogSvc = SingletonProvider<SmsLogSvc>.Instance;
        //log4net对象
        public static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>
        /// 充值账户验证1. 请求ip是否白名单, 2. 是否存在, 3. 是否禁用，4.是否在有效期， 5. 是否有额度
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <param name="rechAmt">本次充值金额(元)</param>
        /// <param name="user">充值账户对象</param>
        /// <param name="ret">0正常，返回结果-1用户不存在,-4账户禁用  -5ip不在白名单 ，-6不在有效期，-7额度不足</param>
        /// <returns></returns>
        protected bool UserValidate(HttpRequestBase request, decimal rechAmt, ReUser user, out int ret)
        {
            ret = 10;
            bool isValidate = false;
            if (user != null && user.UserID > 0)
            {
                string requestIp = request.UserHostAddress;
                string[] IpWhiteList = user.RechIpWhiteList.Split(',');
                if (requestIp.StartsWith("192.168") || IpWhiteList.Contains(requestIp)) //本地ip调用，或者ip在白名单
                {
                    if (user.RechStatus > 0) //是否禁用
                    {
                        DateTime now = DateTime.Now;
                        if (user.BeginTime < now && user.EndTime >= now)
                        {
                            //如果剩余额度小于等于预警额度，需要给项目经理发送短信提醒(30分钟一次)
                            if ((user.RechStock - rechAmt) <= user.RechAlarm && user.AdminId != null && user.AdminId > 0) //达到预警额
                            {
                                var adm = admSvc.FirstAdminById(user.AdminId.Value);
                                SmsLog sl = new SmsLog()
                                {
                                    AdminId = user.AdminId,
                                    Mobile = adm.Mobile,
                                    ReUserId = user.UserID,
                                    SendStatus = 0,
                                    SendTime = System.DateTime.Now,
                                    SmsContent = "瀚微充值平台-项目" + user.UserTitle + "预算额度剩余" + (user.RechStock - rechAmt) + "元，请及时处理",
                                    SmsType = (int)SmsTypeEnum.SysSms
                                };
                                smsLogSvc.SendAlarmSms(sl);
                            }
                            //判断额度
                            if (user.RechStock > rechAmt)
                            {
                                //判断当日额度，超额不在充值
                                if (user.RechDayTotal != null && user.RechDayTotal > 0)
                                {
                                    DateTime beginTime = DateTime.Now.Date;
                                    DateTime endTime = DateTime.Now;
                                    decimal todayTotal = rs.SumRechAmountByReUserAndTime(user.UserID, beginTime, endTime);
                                    if ((todayTotal + rechAmt) > user.RechDayTotal)
                                    {
                                        ret = -7;//当日额度不足
                                    }
                                    else
                                    {
                                        isValidate = true;
                                    }
                                }
                                else
                                {
                                    isValidate = true;
                                }
                            }
                            else
                            {
                                ret = -7;//总额度不足
                            }
                        }
                        else
                        {
                            ret = -6; //不在有效期
                        }
                    }
                    else
                    {
                        ret = -4;//账户禁用
                    }
                }
                else
                {
                    ret = -5;//ip不在白名单
                }
            }
            else
            {
                ret = -1; //账户不存在
            }
            return isValidate;
        }
    }
}